package legumes.shared.domain;

import java.io.Serializable;

import javax.persistence.Embedded;
import javax.persistence.Id;

public class MenuItem implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Id private long id = -1;

	// Either onSaleProduct or menuProduct but not both : one must be null
	private OnSaleProduct onSaleProduct;

	@Embedded private Quantity quantity;

	public MenuItem(MenuItem menuItem_) {
		setId(menuItem_.getId());
		setOnSaleProduct(menuItem_.getOnSaleProduct());
		setQuantity(new Quantity(menuItem_.getQuantity()));
	}

	public MenuItem() {
	}

	public Cost computeRealPrice() {
		Unit menuUnit = getQuantity().getUnitAsEnum();
		Unit productUnit = getOnSaleProduct().getPrice().getQuantity()
				.getUnitAsEnum();

		float price = menuUnit.convertTo(getQuantity().getValue(), productUnit)
				* getOnSaleProduct().getPrice().getAmount()
				/ getOnSaleProduct().getPrice().getQuantity().getValue();

		return new Cost(price, true);
	}

	@Override
	public boolean equals(Object obj) {
		MenuItem mi = (MenuItem) obj;
		return getOnSaleProduct().equals(mi.getOnSaleProduct());
	}

//	/**
//	 * Indicates if the item is based on a real onSaleProduct or on a specific
//	 * onSaleProduct name created just for this menu.
//	 * 
//	 * @return
//	 */
//	public boolean hasRealProduct() {
//		return getOnSaleProduct() != null;
//	}

//	/**
//	 * @return the menuProduct
//	 */
//	public String getMenuProduct() {
//		return menuProduct;
//	}
//
//	/**
//	 * @param menuProduct
//	 *            the menuProduct to set
//	 */
//	public void setMenuProduct(String menuProduct) {
//		this.menuProduct = menuProduct;
//	}

	/**
	 * @return the id
	 */
	public long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(long id) {
		this.id = id;
	}

	/**
	 * @return the quantity
	 */
	public Quantity getQuantity() {
		return quantity;
	}

	/**
	 * @param quantity
	 *            the quantity to set
	 */
	public void setQuantity(Quantity quantity) {
		this.quantity = quantity;
	}

	@Override
	public String toString() {
		return "[MenuItem " + "onSaleProduct=" + getOnSaleProduct()
				+ " quantity=" + getQuantity() + " menuProduct="
				+ getOnSaleProduct().getProduct().getName() + "]";
	}

	public OnSaleProduct getOnSaleProduct() {
		return onSaleProduct;
	}

	public String getArticleName() {
		return getOnSaleProduct().getProduct().getName();
	}

	public void setOnSaleProduct(OnSaleProduct onSaleProduct) {
		this.onSaleProduct = onSaleProduct;
	}

	public OrderPartProductItem toOrderPartProductItem() {
		OrderPartProductItem item = new OrderPartProductItem();
		item.setOnSaleProduct(getOnSaleProduct());
		item.setQuantity(new Quantity(getQuantity()));
		return item;
	}

}
